package com.wp.www.service.impl;


import com.wp.www.entity.account.Account;
import com.wp.www.repository.AccountMapper;
import com.wp.www.service.AccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    /**
     * 修改指定账户余额
     * */
    @Override
    public Integer deduct(String userId, BigDecimal money) {
        Account accountByUserId = findAccountByUserId(userId);
        if (accountByUserId == null){
            throw new RuntimeException("不存在该用户！！！");
        }else {
            BigDecimal balance = accountByUserId.getMoney();
            /**
             * 判断余额是否充足
             * */
            int compareFlag = balance.compareTo(money);
            if (compareFlag<0){
                throw new RuntimeException("账户余额不足！！！");
            }else {
                money = balance.subtract(money);
            }
        }
        return accountMapper.deduct(userId,money);
    }

    @Override
    public Account findAccountByUserId(String userId) {
        return accountMapper.findAccountByUserId(userId);
    }


}
